#!/usr/bin/perl -wc

package RFCMail::ESMTP::Server::Policy::RequireRoutableSender;

use strict;
use RFCMail::ESMTP::Object::ReversePath;


sub mail_from {
    my $self = shift;
    my ($from) = @_;
    my $super = $self->SUPER::mail_from(@_);
    return ($super)
	if exists($self->{require_routable_sender}) and not $self->{require_routable_sender};
    my $mxdata = $self->{state}->{sender_mx};
    if ($from->is_path and not $mxdata) {
	$self->logmsg(e => "no mxdata available to check that sender is routable");
	return ($super);
    }
    my $result = eval {
	return ([250, "Null sender acceptable"])
	    if not $from->is_path;
	my $d = $from->domain;
	return ([451, "4.1.8 Error (".$mxdata->status.") resolving $d as mail domain"])
	    if $mxdata->status =~ /^TF/;
	return ([553, "5.1.8 Sender unacceptable: $d is not a routable mail domain"])
	    if $mxdata->status =~ /^NO/;
	return ([250, "Sender address acceptable"]);
    };
    return ($self->select_reply($super, $result));
}


1;
